package com.boot.unity.api.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.wx.utils.IpAddress;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class WxStorageService {
    @Value("${serv.servurl}")
    private String servurl;


    public Object upload(MultipartFile file, String key) throws IOException {
        String contentType = file.getContentType();
        OkHttpClient client = new OkHttpClient();
        MediaType MEDIA_TYPE_PNG = MediaType.parse(contentType);
        RequestBody fileBody = RequestBody.create(MEDIA_TYPE_PNG, file.getBytes());
        RequestBody formBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getOriginalFilename(), fileBody)
                .addFormDataPart("key", key)
                .build();

        Request request = new Request.Builder()
                .url(servurl+"/wx/storage/upload")
                .post(formBody)
                .build();
        String strResult = "";
        try (Response response = client.newCall(request).execute()) {
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        }
    }

    public Object uploadBmImg(MultipartFile file, String cardNo, String key) throws IOException {
        String contentType = file.getContentType();
        OkHttpClient client = new OkHttpClient();
        MediaType MEDIA_TYPE_PNG = MediaType.parse(contentType);
        RequestBody fileBody = RequestBody.create(MEDIA_TYPE_PNG, file.getBytes());
        RequestBody formBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getOriginalFilename(), fileBody)
                .addFormDataPart("cardNo", cardNo)
                .addFormDataPart("key", key)
                .build();
        Request request = new Request.Builder()
                .url(servurl+"/wx/storage/uploadBmImg")
                .post(formBody)
                .build();
        String strResult = "";
        try (Response response = client.newCall(request).execute()) {
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        }
    }

    public Object cardupload(MultipartFile file, String key) throws IOException {
        String contentType = file.getContentType();
        OkHttpClient client = new OkHttpClient();
        MediaType MEDIA_TYPE_PNG = MediaType.parse(contentType);
        RequestBody fileBody = RequestBody.create(MEDIA_TYPE_PNG, file.getBytes());
        RequestBody formBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getOriginalFilename(), fileBody)
                .addFormDataPart("key", key)
                .build();

        Request request = new Request.Builder()
                .url(servurl+"/wx/storage/cardupload")
                .post(formBody)
                .build();
        String strResult = "";
        try (Response response = client.newCall(request).execute()) {
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        }
    }
}
